home *** CD-ROM | disk | FTP | other *** search
- /* buffer.c */
-
- #include <stdlib.h>
- #include <string.h>
- #include "SCSIPrototypes.h"
- #include "SCSIDefines.h"
- #include "Defines.h"
- #include "Prototypes.h"
-
-
- BUFFER *BufferOpen(void)
- {
- BUFFER *ptr;
- ptr = (BUFFER *)malloc(sizeof(BUFFER));
- if (ptr == NULL) return ptr;
- ptr->index = sizeof(ptr->buffer);
- ptr->write = FALSE;
- return ptr;
- }
-
- short BufferRead(BUFFER *ptr, char *data, long count)
- {
- long i = 0;
-
- if (ptr == NULL) return ERROR;
- if (ptr->write) return ERROR;
- while (i < count)
- {
- if (ptr->index == sizeof(ptr->buffer))
- {
- if (ReadBlocks(ptr->buffer, BLOCKING) != OK) return ERROR;
- ptr->index = 0;
- }
- *data++ = ptr->buffer[ptr->index++];
- i++;
- }
- return OK;
- }
-
- short BufferWrite(BUFFER *ptr, char *data, long count)
- {
- long i = 0;
-
- if (ptr == NULL) return ERROR;
- ptr->write = TRUE;
- if (ptr->index == sizeof(ptr->buffer)) ptr->index = 0;
- while (i < count)
- {
- ptr->buffer[ptr->index++] = *data++;
- i++;
- if (ptr->index == sizeof(ptr->buffer))
- {
- if (WriteBlocks(ptr->buffer, BLOCKING) != OK) return ERROR;
- ptr->index = 0;
- }
- }
- return OK;
- }
-
- void BufferClose(BUFFER *ptr)
- {
- if (ptr->write)
- {
- if (ptr->index)
- {
- memset(&(ptr->buffer[ptr->index]), 0, sizeof(ptr->buffer) - ptr->index);
- WriteBlocks(ptr->buffer, BLOCKING);
- }
- }
- free(ptr);
- WriteMark(1);
- }
-